SlideShare a Scribd company logo
Class No.07  Data Structures http://ecomputernotes.com
Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com
Infix to Postfix Note that the postfix form an expression does not require parenthesis. Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. The postfix forms are: 4+3*5 435*+  (4+3)*5 43+5* http://ecomputernotes.com
Evaluating Postfix  Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.  http://ecomputernotes.com
Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop(); http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2   7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’.  The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. http://ecomputernotes.com
Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. http://ecomputernotes.com
Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE  Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis.
Converting Infix to Postfix Stack s; While( not end of input ) { c = next input character;  if( c is an operand ) add c to postfix string; else { while( !s.empty() && prcd(s.top(),c) ){ op = s.pop();  add op to the postfix string; } s.push( c ); } while( !s.empty() ) { op = s.pop(); add op to postfix string; }
Converting Infix to Postfix Example: A + B * C symb postfix stack A A
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + *
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + *
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * +
Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * +
Converting Infix to Postfix Handling parenthesis When an open parenthesis ‘(‘ is read, it must be pushed on the stack. This can be done by setting prcd(op,‘(‘ ) to be FALSE.  Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack.
Converting Infix to Postfix When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string. To do this, prcd( op,’)’ ) == TRUE. Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE. Need to change line 11 of the algorithm.
Converting Infix to Postfix if( s.empty() || symb != ‘)’ )  s.push( c ); else s.pop(); // discard the ‘(‘  prcd( ‘(‘, op ) = FALSE for any operator prcd( op, ‘)’ ) = FALSE for any operator other than ‘)’ prcd( op, ‘)’ ) = TRUE for any operator  other than ‘(‘ prcd( ‘)’, op ) = error for any operator.

More Related Content

PPT
Computer notes - Postfix
PDF
Ds stack 03
PPTX
Prefix, Infix and Post-fix Notations
PPTX
My lecture infix-to-postfix
PPT
Application of Stacks
PPT
Expression evaluation
PPT
Circular queues
PPT
Computer notes - Postfix
Ds stack 03
Prefix, Infix and Post-fix Notations
My lecture infix-to-postfix
Application of Stacks
Expression evaluation
Circular queues

What's hot (18)

PPT
Infix prefix postfix
DOC
Infix to-postfix examples
PPT
Conversion of Infix To Postfix Expressions
PPTX
Scope and closures
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PPTX
Data structures
PPTX
Stack and queue -polish notations
PPT
Infix to Postfix Conversion Using Stack
PDF
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
PPTX
Application of Stack - Yadraj Meena
PPTX
Infix postfixcoversion
PPTX
Polish
PPTX
Nested loops
PDF
Reliability Patterns for Fun and Profit
PPT
Computer notes data structures - 9
PPT
Whats new in_csharp4
DOC
35787646 system-software-lab-manual
PDF
C言語静的解析ツールと Ruby 1.9 trunk
Infix prefix postfix
Infix to-postfix examples
Conversion of Infix To Postfix Expressions
Scope and closures
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
Data structures
Stack and queue -polish notations
Infix to Postfix Conversion Using Stack
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Application of Stack - Yadraj Meena
Infix postfixcoversion
Polish
Nested loops
Reliability Patterns for Fun and Profit
Computer notes data structures - 9
Whats new in_csharp4
35787646 system-software-lab-manual
C言語静的解析ツールと Ruby 1.9 trunk
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 16
computer notes - Data Structures - 32
computer notes - Data Structures - 4
computer notes - Data Structures - 22
computer notes - Data Structures - 26
computer notes - Data Structures - 29
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 33
computer notes - Data Structures - 39
computer notes - Data Structures - 19
computer notes - Data Structures - 38
computer notes - Data Structures - 35
computer notes - Data Structures - 9
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 5
computer notes - Data Structures - 15
computer notes - Data Structures - 31
computer notes - Data Structures - 21
computer notes - Data Structures - 16
Ad

Similar to computer notes - Data Structures - 7 (20)

PPT
data structure and algorithm by bomboat_3
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
Evaluation of postfix expression using stack
PPTX
week9-prefixinfixandpostfixnotations-191013065821.pptx
PPTX
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
PPTX
Lecture_04.2.pptx
PPT
MO 2020 DS Stacks 3 AB.ppt
PPTX
DS MOD2 (1) (1).pptx
PDF
stack-111104232459-phpapp02.pdf
PPTX
COMP1603 Stacks and RPN 2023 Recording (2).pptx
PDF
1.3- infix-ti-postfix.pdf
PPTX
Lec5-Stack-bukc-28022024-112316am (1) .pptx
PDF
Data Structures And Algorithms(stacks queues)
PPSX
Data structure_Stack Introduction & app.
PPTX
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
PDF
Infix to postfix expression in ds
PPTX
Unit 2 application of stack
PPTX
Topic 2_revised.pptx
PDF
computer notes - Evaluating postfix expressions
data structure and algorithm by bomboat_3
Data Structure and Algorithms by Sabeen Memon03.pptx
Evaluation of postfix expression using stack
week9-prefixinfixandpostfixnotations-191013065821.pptx
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
Lecture_04.2.pptx
MO 2020 DS Stacks 3 AB.ppt
DS MOD2 (1) (1).pptx
stack-111104232459-phpapp02.pdf
COMP1603 Stacks and RPN 2023 Recording (2).pptx
1.3- infix-ti-postfix.pdf
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Data Structures And Algorithms(stacks queues)
Data structure_Stack Introduction & app.
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Infix to postfix expression in ds
Unit 2 application of stack
Topic 2_revised.pptx
computer notes - Evaluating postfix expressions

More from ecomputernotes (18)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
Programs and apps: productivity, graphics, security and other tools
cuic standard and advanced reporting.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

computer notes - Data Structures - 7

  • 1. Class No.07 Data Structures http://ecomputernotes.com
  • 2. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+ http://ecomputernotes.com
  • 3. Infix to Postfix Note that the postfix form an expression does not require parenthesis. Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. The postfix forms are: 4+3*5 435*+ (4+3)*5 43+5* http://ecomputernotes.com
  • 4. Evaluating Postfix Each operator in a postfix expression refers to the previous two operands. Each time we read an operand, we push it on a stack. When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack. http://ecomputernotes.com
  • 5. Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop(); http://ecomputernotes.com
  • 6. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 http://ecomputernotes.com
  • 7. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 http://ecomputernotes.com
  • 8. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 http://ecomputernotes.com
  • 9. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 http://ecomputernotes.com
  • 10. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 http://ecomputernotes.com
  • 11. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 http://ecomputernotes.com
  • 12. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 http://ecomputernotes.com
  • 13. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 http://ecomputernotes.com
  • 14. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 http://ecomputernotes.com
  • 15. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 http://ecomputernotes.com
  • 16. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 http://ecomputernotes.com
  • 17. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2 http://ecomputernotes.com
  • 18. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 http://ecomputernotes.com
  • 19. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 http://ecomputernotes.com
  • 20. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
  • 21. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52 http://ecomputernotes.com
  • 22. Converting Infix to Postfix Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’. The postfix versions are ‘ABC*+’ and ‘AB+C*’. The order of operands in postfix is the same as the infix. In scanning from left to right, the operand ‘A’ can be inserted into postfix expression. http://ecomputernotes.com
  • 23. Converting Infix to Postfix The ‘+’ cannot be inserted until its second operand has been scanned and inserted. The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence. http://ecomputernotes.com
  • 24. Converting Infix to Postfix In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
  • 25. Converting Infix to Postfix prcd(‘*’,’+’) is TRUE prcd(‘+’,’+’) is TRUE prcd(‘+’,’*’) is FALSE Here is the algorithm that converts infix expression to its postfix form. The infix expression is without parenthesis.
  • 26. Converting Infix to Postfix Stack s; While( not end of input ) { c = next input character; if( c is an operand ) add c to postfix string; else { while( !s.empty() && prcd(s.top(),c) ){ op = s.pop(); add op to the postfix string; } s.push( c ); } while( !s.empty() ) { op = s.pop(); add op to postfix string; }
  • 27. Converting Infix to Postfix Example: A + B * C symb postfix stack A A
  • 28. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A +
  • 29. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB +
  • 30. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + *
  • 31. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + *
  • 32. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * +
  • 33. Converting Infix to Postfix Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * +
  • 34. Converting Infix to Postfix Handling parenthesis When an open parenthesis ‘(‘ is read, it must be pushed on the stack. This can be done by setting prcd(op,‘(‘ ) to be FALSE. Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack.
  • 35. Converting Infix to Postfix When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string. To do this, prcd( op,’)’ ) == TRUE. Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE. Need to change line 11 of the algorithm.
  • 36. Converting Infix to Postfix if( s.empty() || symb != ‘)’ ) s.push( c ); else s.pop(); // discard the ‘(‘ prcd( ‘(‘, op ) = FALSE for any operator prcd( op, ‘)’ ) = FALSE for any operator other than ‘)’ prcd( op, ‘)’ ) = TRUE for any operator other than ‘(‘ prcd( ‘)’, op ) = error for any operator.

Editor's Notes

  • #3: End of lecture 6
  • #4: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #5: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #6: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #7: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #8: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #9: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #10: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #11: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #12: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #13: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #14: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #15: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #18: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #19: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #20: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #21: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #22: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #23: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #24: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #25: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #26: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #27: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #28: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #29: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #30: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #31: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #32: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #33: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #34: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #35: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #36: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #37: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.